<template>
  <div class="content">
    <el-button type="primary" @click="show = true" v-if="$_permissions()=='2'||$_permissions()=='3'">新增</el-button>
    <div class="table">
      <el-table :data="tableData" border style="width: 40%">
       <!--  <el-table-column prop="id" label="ID" align="center"></el-table-column> -->
        <el-table-column prop="name" label="名称" align="center"></el-table-column>
        <el-table-column prop="price" label="价格" align="center"></el-table-column>
        <el-table-column fixed="right" v-if="$_permissions()=='2'||$_permissions()=='3'" label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row, true)" type="text" size="small">编辑</el-button>
            <el-button type="text" size="small" @click="handleClick(scope.row, false)" v-if="$_permissions()=='3'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>
    <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.start"
          :page-sizes="[10, 25, 50, 100]"
          :page-size="pagination.nums"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total">
        </el-pagination>
    <el-dialog :title="ruleForm.id ? '编辑' : '添加'" :visible.sync="show" width="30%" @close="handleClose">
      <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="名称" prop="name"><el-input v-model="ruleForm.name"></el-input></el-form-item>
          <el-form-item label="价格" prop="price"><el-input type="number" v-model="ruleForm.price"></el-input></el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">{{ ruleForm.id ? '保存' : '创建' }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { showHC, delHC, updatetHC, insertHC } from '@/api/test.js';
import pagination from "@/mixins/page.js"
export default {
  name: 'hongcaitj',
  mixins:[pagination],
  data() {
    return {
      tableData: [],
      show: false,
      ruleForm: {},
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        price: [{ required: true, message: '请输入价格', trigger: 'blur' }]
      }
    };
  },
  methods: {
    handleClick(row, status) {
      //status true:编辑 false:删除
      if (status) {
        //编辑
        this.ruleForm = { ...row };
        this.show = true;
      } else {
        //删除
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            delHC({ id: row.id }).then(res => {
              console.log(res);
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success('删除成功');
                this.getList();
              } else {
                this.$message.error('删除失败');
              }
            });
          })
          .catch(() => {});
      }
    },
    getList() {
      //获取列表
      showHC(this.pagination).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.pagination.total = res.total
      });
    },
    submitForm() {
      //提交请求
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          if (this.ruleForm.id) {
            //更新
            updatetHC(this.ruleForm).then(res => {
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success(res.msg);
                this.handleClose();
                this.getList();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //添加
            insertHC(this.ruleForm).then(res => {
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success(res.msg);
                this.handleClose();
                this.getList();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
    handleClose() {
      //弹窗关闭回调 -- 清空表单数据
      this.ruleForm = {};
      this.show = false;
      this.$refs["ruleForm"].resetFields();
    }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style lang="scss" scoped>
.content {
}
</style>
